from app.models.song import Song
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import logging
import os


class AddSongPage(webapp.RequestHandler):
    def get(self, *args):
        loginUrl = users.create_login_url(self.request.uri)
        path = os.path.join(os.path.dirname(__file__), '../views/add_song.html')
        self.response.out.write(template.render(path, {'loginUrl':loginUrl}))
        
    def post(self):
        if not users.get_current_user():
            self.redirect('error.html')
            return
        name = self.request.get('name')
        artist = self.request.get('artist')
        content = self.request.get('content')
        song = Song()
        song.name = name
        song.song = content
        #song.artist = None
        song.author = users.get_current_user()
        song.put()
        
        path = os.path.join(os.path.dirname(__file__), '../views/add_song.html')
        context = {'artist' : artist, 'content':content}
        logging.error(content)
        self.response.out.write(template.render(path, context))
        